package com.yupi.usercenter.exception;


import com.yupi.usercenter.common.BaseResponse;
import com.yupi.usercenter.common.ErrorCode;
import com.yupi.usercenter.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e) {
        log.error("businessException: " + e.getMessage(), e);
        return ResultUtils.error(e.getMessage());
    }
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e) {
        log.error("businessException: " + e.getMessage(), e);
        return ResultUtils.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse exceptionHandler(Exception e) {
        log.error("exception: " + e.getMessage(), e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public BaseResponse nullPointerExceptionHandler(NullPointerException e) {
        log.error("nullPointerException: " + e.getMessage(), e);
        return ResultUtils.error(ErrorCode.NULL_ERROR.getCode(), e.getMessage());
    }
    @ExceptionHandler(ArithmeticException.class)
    public BaseResponse arithmeticExceptionHandler(ArithmeticException e) {
        log.error("arithmeticException: " + e.getMessage(), e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public BaseResponse arrayIndexOutOfBoundsExceptionHandler(ArrayIndexOutOfBoundsException e) {
        log.error("arrayIndexOutOfBoundsException: " + e.getMessage(), e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR.getCode(), e.getMessage());
    }
}
